unit QBLinkProp_DEVEXP;

interface

{$IFDEF USE_DEVEXPRESS}
uses
  {$IFDEF VCL16}
    Winapi.Windows,
    Winapi.Messages,
    System.SysUtils,
    System.Classes,
    Vcl.Graphics,
    Vcl.Controls,
    Vcl.Forms,
    Vcl.StdCtrls,
    Vcl.Menus,
  {$ELSE}
    Windows,
    Messages,
    SysUtils,
    Classes,
    Graphics,
    Controls,
    Forms,
    StdCtrls,
    Menus,
  {$ENDIF}
  QBLinks,
  cxLookAndFeelPainters,
  cxButtons,
  cxControls,
  cxContainer,
  cxEdit,
  cxCheckBox,
  cxTextEdit,
  cxMaskEdit,
  cxDropDownEdit,
  cxGraphics;

var
  CurrentLnk: TObject;

type

  TfmLinkProp = class(TForm)
    laCaption: TLabel;
    laLeftField: TLabel;
    laRightField: TLabel;
    buOK: TcxButton;
    buCancel: TcxButton;
    chLeftOuter: TcxCheckBox;
    chRightOuter: TcxCheckBox;
    cbConditions: TcxComboBox;
    procedure FormShow(Sender: TObject);
    procedure buOKClick(Sender: TObject);
    procedure FormCanResize(Sender: TObject; var NewWidth,
      NewHeight: Integer; var Resize: Boolean);
    procedure FormResize(Sender: TObject);
    procedure chRightOuterClick(Sender: TObject);
    procedure chLeftOuterClick(Sender: TObject);
  private
    FMinWidth: Integer;
    FFullOuterJoinSupported: boolean;
    procedure CalcMinWidth;
    function GetCompareOperatorList: string;
    procedure SetCompareOperatorList(const Value: string);
    procedure SetFullOuterJoinSupported(const Value: boolean);
  public
    property CompareOperatorList: string read GetCompareOperatorList write
      SetCompareOperatorList;
    property FullOuterJoinSupported: boolean read FFullOuterJoinSupported
      write SetFullOuterJoinSupported;
  end;
{$ENDIF}
implementation
{$IFDEF USE_DEVEXPRESS}

uses QBFloatPanel, QBParser, QBWindow, QBResource, QBFloatTable;

{$R *.DFM}

procedure TfmLinkProp.FormShow(Sender: TObject);

  function GetTableName(Table: TQBTable): WideString;
  begin
    Result := Table.TableName;
    if (Table.Alias <> '') and (Table.Alias <> Table.TableName) then
      Result := Result + ' (' + Table.Alias + ')';
  end;

var
  Lnk: TQBLink;
begin
  if (CurrentLnk = nil) or not (CurrentLnk is TQBLink) then
    raise Exception.Create(rsLinkPropInvalidLink);
  Lnk := CurrentLnk as TQBLink;
  // ayznew
  {laCaption.Caption := rsLinkPropJoinTables + Lnk.Join.TableName[0] + ' ' +
    rsLinkPropAnd + ' ' + Lnk.Join.TableName[1];}
  laCaption.Caption := rsLinkPropJoinTables + GetTableName(Lnk.Join.Table[0]) + ' ' +
    rsLinkPropAnd + ' ' + GetTableName(Lnk.Join.Table[1]);
  // \ayznew
  laLeftField.Caption := Lnk.Field1.Caption;
  laRightField.Caption := Lnk.Field2.Caption;
  chLeftOuter.Caption := rsLinkPropIncludeAllFrom + ' ' + Lnk.Join.TableName[0];
  chRightOuter.Caption := rsLinkPropIncludeAllFrom + ' ' + Lnk.Join.TableName[1];
  chLeftOuter.Checked := false;
  chRightOuter.Checked := false;
  if (Lnk.Join.Kind = qbjLOuterJoin) or (Lnk.Join.Kind = qbjFOuterJoin) then
    chLeftOuter.Checked := true;
  if (Lnk.Join.Kind = qbjROuterJoin) or (Lnk.Join.Kind = qbjFOuterJoin) then
    chRightOuter.Checked := true;
  cbConditions.Text := Lnk.Condition;
  CalcMinWidth;
  if FMinWidth > Width then
    Width := FMinWidth;
  FormResize(nil);
end;

procedure TfmLinkProp.buOKClick(Sender: TObject);
var
  Link: TQBLink;
  JoinDef: TQBJoinDef;
  J: integer;
  Parser: TQBSelectParser;
begin
  Link := TQBLink(CurrentLnk);
  if not Assigned(Link) then
    Exit;

  Parser := TCustomQueryBuilder(Link.Parent.Owner).Parser;
  JoinDef := Parser.JoinDefs.Find(
    Link.Join.Table[0].Alias, Link.Field1.Caption,
    Link.Join.Table[1].Alias, Link.Field2.Caption);

  if chLeftOuter.Checked and chRightOuter.Checked then
  begin
    Link.Join.Kind := qbjFOuterJoin;
    if Assigned(JoinDef) then
      JoinDef.Kind := qbjFOuterJoin;
  end
  else if chLeftOuter.Checked then
  begin
    Link.Join.Kind := qbjLOuterJoin;
    if Assigned(JoinDef) then
      JoinDef.Kind := qbjLOuterJoin;
  end
  else if chRightOuter.Checked then
  begin
    Link.Join.Kind := qbjROuterJoin;
    if Assigned(JoinDef) then
      JoinDef.Kind := qbjROuterJoin;
  end
  else
  begin
    Link.Join.Kind := qbjInnerJoin;
    if Assigned(JoinDef) then
      JoinDef.Kind := qbjInnerJoin;
  end;
  Link.Condition := cbConditions.Text;

  if Assigned(JoinDef) then
  begin
    JoinDef.Operator := cbConditions.Text;
    for J := 0 to Parser.JoinDefs.Count - 1 do
      if (Parser.JoinDefs[J].TableLeft = Link.Join.Table[0].Alias) and
        (Parser.JoinDefs[J].TableRight = Link.Join.Table[1].Alias) then
        Parser.JoinDefs[J].Kind := Link.Join.Kind;
  end;
end;

procedure TfmLinkProp.FormCanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
begin
  Resize := true;
  if NewHeight <> Height then
    Resize := false;
  if NewWidth < FMinWidth then
    Resize := false;
end;

procedure TfmLinkProp.CalcMinWidth;
var
  lFieldWidth, btnWidth: Integer;
  DC: HDC;
  sz: TSize;
begin
  DC := GetDC(chLeftOuter.Handle);
  try
    SelectObject(DC, chLeftOuter.Style.Font.Handle);
    GetTextExtentPoint32(DC, @chLeftOuter.Caption[1],
      Length(chLeftOuter.Caption), sz);
    sz.cx := sz.cx + 20;
    sz.cx := sz.cx + 5; // +
    chLeftOuter.Width := sz.cx;
    btnWidth := sz.cx;
    GetTextExtentPoint32(DC, @chRightOuter.Caption[1],
      Length(chRightOuter.Caption), sz);
    sz.cx := sz.cx + 20;
    sz.cx := sz.cx + 5; // +
    chRightOuter.Width := sz.cx;
  finally
    ReleaseDC(chLeftOuter.Handle, DC);
  end;
  if btnWidth < sz.cx then
    btnWidth := sz.cx;
  btnWidth := btnWidth * 2 + 28;
  if laLeftField.Width > laRightField.Width then
    lFieldWidth := laLeftField.Width
  else
    lFieldWidth := laRightField.Width;
  lFieldWidth := lFieldWidth * 2 + cbConditions.Width + 22;
  FMinWidth := laCaption.Canvas.TextWidth(laCaption.Caption) + 22;
  if FMinWidth < lFieldWidth then
    FMinWidth := lFieldWidth;
  if FMinWidth < btnWidth then
    FMinWidth := btnWidth;
  if FMinWidth < 360 then
    FMinWidth := 360;
end;

procedure TfmLinkProp.FormResize(Sender: TObject);
begin
  laLeftField.Left := (Width - cbConditions.Width) div 2 - 1 -
    laLeftField.Width;
  laRightField.Left := (Width + cbConditions.Width) div 2 + 1;
  cbConditions.Left := (Width - cbConditions.Width) div 2;
  chLeftOuter.Left := Width div 2 - 4 - chLeftOuter.Width;
  chRightOuter.Left := Width div 2 + 4;
  buOK.Left := Width div 2 - 20 - buOK.Width;
  buCancel.Left := Width div 2 + 20;
end;

function TfmLinkProp.GetCompareOperatorList: string;
begin
  Result := cbConditions.Properties.Items.CommaText;
end;

procedure TfmLinkProp.SetCompareOperatorList(const Value: string);
begin
  cbConditions.Properties.Items.CommaText := Value;
end;

procedure TfmLinkProp.SetFullOuterJoinSupported(const Value: boolean);
begin
  FFullOuterJoinSupported := Value;
end;

procedure TfmLinkProp.chRightOuterClick(Sender: TObject);
begin
  if not FFullOuterJoinSupported and chRightOuter.Checked and
    chLeftOuter.Checked then
    chLeftOuter.Checked := false;
end;

procedure TfmLinkProp.chLeftOuterClick(Sender: TObject);
begin
  if not FFullOuterJoinSupported and chLeftOuter.Checked and
    chRightOuter.Checked then
    chRightOuter.Checked := false;
end;


{$ENDIF}
end.
